package com.zm.shop.goods.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.zm.shop.goods.entity.GoodsPngEntity;
import com.zm.shop.goods.service.GoodsPngService;
import com.zm.shop.stock.entity.StockEntity;
import com.zm.shop.stock.service.StockService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.zm.shop.goods.entity.GoodsEntity;
import com.zm.shop.goods.service.GoodsService;
import com.zm.shop.common.utils.PageUtils;
import com.zm.shop.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 商品管理
 *
 * @author Java2103
 * @email Java2103@qq.com
 * @date 2021-08-03 22:03:00
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private StockService stockService;

    @Autowired
    private GoodsPngService goodsPngService;


    @Value("${fdfs.fastPath}")
    private String fastPath;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @RequestMapping("/fileUpload")
    public R fileUpload(MultipartFile file) throws Exception{
        //获取文件后缀
        String fileSuffix= FilenameUtils.getExtension(file.getOriginalFilename());
        //获取文件大小
        long size = file.getSize();
        //上传到服务器
        InputStream ips = file.getInputStream();
        //返回图片存储地址对象
        StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(ips, size, fileSuffix, null);
        //获取图片存储地址（内部地址）
        String url = storePath.getFullPath();
        //返回一个完整地址给前端访问
        System.out.println("放回的图片地址是"+url);
        return R.ok().put("url",fastPath+url);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        //获得分页类
        PageUtils page = goodsService.queryPage(params);
        //获得全部商品信息
        List<GoodsEntity> goodsEntityList = (List<GoodsEntity>) page.getList();
        //通过ID获得商品图片集合goodsPngEntityList 获得商品库存
        for (GoodsEntity goodsEntity : goodsEntityList) {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("gid",goodsEntity.getId());
            List<GoodsPngEntity> goodsPngEntityList = goodsPngService.list(queryWrapper);
            goodsEntity.setGoodsPngEntityList(goodsPngEntityList);

        }


        //添加分页总数据

        Integer totalCount=page.getList().size();
        page.setTotalCount(totalCount);
        page.setList(goodsEntityList);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		GoodsEntity goodsEntity = goodsService.getById(id);
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.eq("gid",goodsEntity.getId());
        List<GoodsPngEntity> goodsPngEntityList = goodsPngService.list(queryWrapper);
        List<StockEntity> stockEntityList = stockService.list(queryWrapper);
        if (stockEntityList!=null&&stockEntityList.size()>0){
            goodsEntity.setStockEntity(stockEntityList.get(0));
        }
        goodsEntity.setGoodsPngEntityList(goodsPngEntityList);
        return R.ok().put("goods", goodsEntity);
    }


    @GetMapping("/getGoodsEntityById/{id}")
    public GoodsEntity getGoodsEntityById(@PathVariable("id") Integer id){
        GoodsEntity goodsEntity = goodsService.getById(id);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("gid",goodsEntity.getId());
        goodsEntity.setGoodsPngEntityList(goodsPngService.list(queryWrapper));
        goodsEntity.setStockEntity(stockService.getOne(queryWrapper));
        return goodsEntity;
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GoodsEntity goodsEntity){
        System.out.println("前端获得的数据是"+goodsEntity);
        goodsService.saveAll(goodsEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		goodsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
